//
//  XUITabBar.m
//  etionUI
//
//  Created by wjxfhxy on 15/8/13.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XUITabBar.h"

#import "XUITabBarItem.h"

@interface XUITabBar ()
{
    NSDictionary *_buttons;
}

@end

@implementation XUITabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    return self;
}

- (void)dealloc
{
    for(XUITabBarItem *item in super.items)
    {
        [item removeObserver:self forKeyPath:@"badgeString"];
    }
    [_buttons release_mm];
    superdealloc_mm;
}

- (void)setItems:(NSArray *)items
{
    for(XUITabBarItem *item in super.items)
    {
        [item removeObserver:self forKeyPath:@"badgeString"];
    }
    
    [super setItems:items];
    
    ReleaseObj(_buttons);
    
    if(items.count > 0)
    {
        for(XUITabBarItem *item in items)
        {
            [item addObserver:self forKeyPath:@"badgeString" options:NSKeyValueObservingOptionNew context:nil];
        }
        
        NSMutableDictionary *buttons = [NSMutableDictionary new];
        
        NSArray *subviews = self.subviews;
        NSUInteger i = 0;
        NSUInteger j = 0;
        do
        {
            if(j < subviews.count)
            {
                UIView *view = subviews[j];
                if([view isMemberOfClass:NSClassFromString(@"UITabBarButton")])
                {
                    XUITabBarItem *item = items[i];
                    
                    if(item.badgeString.length > 0)
                        [self createBadgeViewWithParentView:view andBadgeString:item.badgeString];
                    
                    buttons[item.title] = view;
                    i ++;
                }
                j ++;
            }
            else
                break;
        }while (i < items.count);
        
        _buttons = buttons;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

- (void)createBadgeViewWithParentView:(UIView*)parentView andBadgeString:(NSString*)badgeString
{
    XUIBadgeView *badgeView = (XUIBadgeView *)[parentView viewWithTag:100000];
    
    if(badgeString.length > 0)
    {
        if(badgeView == nil)
        {
            XUIBadgeViewType badgeViewType = [badgeString isEqualToString:@"point"] ? XUIBadgeViewTypeByPointStyle : XUIBadgeViewTypeByTextStyle;
            
            badgeView = [XUIBadgeView badgeViewWithBadgeViewType:badgeViewType andParentView:parentView byAlignment:XUIBadgeViewAlignmentByTopCenter];
            badgeView.tag = 100000;
            badgeView.badgeViewCenterOffset = CGPointMake(20, 10);
            if(badgeViewType == XUIBadgeViewTypeByTextStyle)
                badgeView.text = badgeString;
        }
        else if([badgeView.text isEqualToString:badgeString] == NO)
        {
            if(badgeView.badgeViewType == XUIBadgeViewTypeByTextStyle && [badgeString isEqualToString:@"point"] == YES)
            {
                [badgeView removeFromSuperview];
                
                badgeView = [XUIBadgeView badgeViewWithBadgeViewType:XUIBadgeViewTypeByPointStyle andParentView:parentView byAlignment:XUIBadgeViewAlignmentByTopCenter];
                badgeView.tag = 100000;
                badgeView.badgeViewCenterOffset = CGPointMake(20, 10);
            }
            else if(badgeView.badgeViewType == XUIBadgeViewTypeByPointStyle && [badgeString isEqualToString:@"point"] == NO)
            {
                [badgeView removeFromSuperview];
                
                badgeView = [XUIBadgeView badgeViewWithBadgeViewType:XUIBadgeViewTypeByTextStyle andParentView:parentView byAlignment:XUIBadgeViewAlignmentByTopCenter];
                badgeView.tag = 100000;
                badgeView.badgeViewCenterOffset = CGPointMake(20, 10);
                badgeView.text = badgeString;
            }
            else
            {
                badgeView.text = badgeString;
            }
        }
    }
    else
    {
        [badgeView removeFromSuperview];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"badgeString"])
    {
        XUITabBarItem *item = object;
        UIView *view = _buttons[item.title];
        [self createBadgeViewWithParentView:view andBadgeString:item.badgeString];
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end
